<?php
namespace Apps\Csoap\Controllers;

use Phalcon\Mvc\Controller;
use Apps\Csoap\Controllers\BaseController;
use PhalconPlus\Common\ResponseResultStatus;
use Apps\Common\Models\Branch;
use Apps\Common\Models\BeautyParlor;
use Apps\Common\Models\BeautyParlorPhotos;
use Apps\Common\Models\BeautyParlorTag;
use Apps\Common\Models\SysAreaZipDistrictPhonecode;
use PhalconPlus\Api\Common\ResultStatus;
use Apps\Common\Models\BeautyParlorTagInfo;
use PhalconPlus\Common\ResponseResult;
use Phalcon\Mvc\Model;
use Apps\Common\Models\BeautyParlorAttr;
use Apps\Common\Models\BeautyParlorService;
use Apps\Common\Libs\Keyword;
use Apps\Common\Models\CosQKeyword;


class OnlineserviceController extends BaseController 
{
    
    private $success = 0;
    private $id = 0;
    
    public function initialize(){
        parent::initialize();
    }
    /**
     * 获取线上店铺服务列表
     * @date: 2016年3月19日 
     * @author: 545
     */
    public function servicelistAction(){
        $this->view->disable();
        $branch_id = $this->getDataItem('shop_id',0);
        if ($branch_id==0){
            $this->senderror(ResultStatus::POST_BODY_PARAM_ERROR,"shop_id不能为空");
        }
        //获取美容院id
    	$bp_id  = BeautyParlor::findFirst("branch_id = {$branch_id}")->bp_id;
    	//根据美容院id获取服务列表
    	$type = intval($this->getDataItem('type'),-10000);//查询类型 1 可预约， 0 已下架
    	$keyword = $this->getDataItem('keyword');//关键字
    	if($type!=-10000){
    	    $type = $type==1?1:-1;
    	    $where[] = "service_is_sell = {$type}";
    	}
    	if(!empty($keyword)){
    	    $where[] = "service_name like '%$keyword%'";
    	}
    	$where = implode(' AND ', $where);
    	$where = $where ==''?'':$where.=" AND ";
    	if(0 < $bp_id) {
    	    $service = BeautyParlorService::query()
    	    ->columns([
    	        'service_id id',
    	        'service_cover cover',
    	        'service_name name',
    	        'service_price price',
    	        'service_unit unit','service_is_sell is_sell','service_addtime addtime'])
    	        ->where($where.'service_state >0 and beauty_parlor_id=:bid:')
    	        ->bind(['bid'=>$bp_id])
    	        ->orderBy('service_addtime DESC')
    	        ->execute()->toArray();
    	    return $this->sendResult(ResultStatus::SUCCESS,$service);
    	} else {
    	    return $this->senderror(ResultStatus::URL_PARAM_CANNOT_EMPTY, '参数异常');
    	}
    }
    /**
     * 获取线上店铺服务详情
     * @date: 2016年3月19日 
     * @author: 545
     */
    public function serviceinfoAction(){
        
        $branch_id = $this->getDataItem('shop_id',0);
        if ($branch_id==0){
            $this->senderror(ResultStatus::POST_BODY_PARAM_ERROR,"shop_id不能为空");
        }
        //获取美容院id
        $bp_id  = BeautyParlor::findFirst("branch_id = {$branch_id}")->bp_id;
        
        $id = intval($this->getDataItem('id',0));
        
        if(0 < $id) {
            /**
             * 查询服务信息
             */
            $service = BeautyParlorService::query()
            ->columns([
                'service_id as id',
                'service_cover as cover',
                'service_name as name',
                'service_price as price',
                'service_unit as unit',
                'service_state as state',
                'service_description as description',
                'service_intro as intro',
                'beauty_parlor_id as bp_id',
                'service_is_sell as is_sell',
                'service_addtime as addtime'
                //'service_state as state'
            ])
            ->where('service_id=:id: AND beauty_parlor_id = :bid:')
            ->bind(['id' => $id,'bid'=>$bp_id])
            ->execute()->getFirst();
        }
        $data = null;
        if($service) {
            $data = [
                'id' => $service->id,
                'cover' => $service->cover, $this->getDi(),
                'name' => $service->name,
                'price' => $service->price,
                'description' => $service->description,
                'intro' =>$service->intro,
                'intro' => \Apps\Common\Libs\BeautyParlor::formatServiceIntro($service->intro, $this->getDi(), true),
                'service_state'=>$service->state,
                'is_sell'=>$service->is_sell,
                'unit' => $service->unit,
                'addtime' => $service->addtime,
            ];
        }
        $this->sendResult(ResultStatus::SUCCESS,$data);
    }
    /**
     * 新增|修改|发布线上店铺服务
     * @date: 2016年3月19日 
     * @author: 545
     */
    public function modifyAction(){
        $branch_id = intval($this -> getDataItem('shop_id',0));
        if ($branch_id==0){
            $this->senderror(ResultStatus::POST_BODY_PARAM_ERROR,"shop_id不能为空");
        }
        //获取美容院id
        $bp_id  = BeautyParlor::findFirst("branch_id = {$branch_id}")->bp_id;
        //服务名称
        $service_name = $this->getDataItem('service_name');
        $service_price = $this->getDataItem('service_price');
        $service_unit = $this->getDataItem('service_unit');
        $is_shell = $this->getDataItem('is_shell',1);
        $is_shell = $is_shell==1?1:-1;
        
        $comment_info = Keyword::Filter($this->getDataItem('content',''));
        $comment_info = trim($comment_info);
        //关键字替换
        $keyword = CosQKeyword::query() -> execute() -> toArray();
        foreach ($keyword as $k => $v){
        
            $comment_info = str_replace($v['keyword'], '***', $comment_info);
        
        }
        $service_id = $this->getDataItem('service_id',0);
        
        
        $bp_cover = $this->getDataItem('service_cover');
        $data = [];
        //如果是新增服务 则检查参数
        if ($service_id==0){
            if (empty($service_name)||empty($bp_cover)||empty($comment_info)||empty($bp_id)){
                $this->senderror(ResultStatus::POST_BODY_PARAM_ERROR,"数据不全");
            }
        }else {//修改服务则验证服务是否存在
            $data = BeautyParlorService::findFirst($service_id);
        }
        
        if($data) {//服务存在则修改
        
            if (!empty($service_name)){
                $temp['service_name'] = $service_name;
            }
            if (!empty($service_price)){
                $temp['service_price'] = $service_price;
            }
            if (!empty($service_unit)){
                $temp['service_unit'] = $service_unit;
            }
            if(!empty($bp_cover)){
                $temp['service_cover'] = $bp_cover;
            }
            if (!empty($comment_info)){
                $temp['service_description'] = $comment_info;
                $temp['service_intro'] = $comment_info;
            }
            $temp['service_is_sell'] = $is_shell;
            $temp['service_state'] = 1;
            $temp['service_addtime'] = date("Y-m-d H:i:s", time());
        
            if ($data->update($temp)){
                $this->id = $data->service_id;
                $this->success = 1;
            }
            else {
                $this->id = $data->service_id;
                $this->success = 0;
            }
        
        
        } else {
        
            //添加服务
            $q = new BeautyParlorService();
            $q->beauty_parlor_id = $bp_id;
            $q->service_cover = $bp_cover;
            $q->service_name = $service_name;
            $q->service_description = $comment_info;
            $q->service_intro = $comment_info;
            $q->service_price = empty($service_price)?0:$service_price;
            $q->service_unit = empty($service_unit)?0:$service_unit;
            $q->service_state = 1;
            $q->service_is_sell = $is_shell;
            $q->service_addtime = date("Y-m-d H:i:s", time());
        
            if ($q->save()){
                $this->id = $q->service_id;
                $this->success = 1;
            }
            else{
                $this->id = $q ->service_id;
                $this->success = 0;
            }
        }
        
        $this->sendResult(ResultStatus::SUCCESS,[
            'id' => $this->id,
            'success' => $this->success
        ]);
    }
}